import { defineStore } from "pinia";
import { ref } from "vue";

// 定义Breadcrumb模块的store
export const useTagStore = defineStore("tag", () => {
  const tagList = ref([
    {
      label: "工作台",
      name: "Home",
      key: "/home",
    },
  ]);

  //退出时清空数据
  const removeTagList = () => {
    tagList.value = [
      {
        label: "工作台",
        name: "Home",
        key: "/home",
      },
    ];
  };

  // 设置动态面包屑
  const setTagList = (newTag) => {
    if (newTag.name !== "Home") {
      // 去重逻辑，防止多个相同的面包屑添加进去
      let result = tagList.value.findIndex((item) => item.name === newTag.name);
      // 如果未找到相同的面包屑，则将newTag添加到tagList中
      result === -1 ? tagList.value.push(newTag) : "";
    }
  };

  // 清除标签
  const clearTags = (newTags) => {
    // 删除tagList中指定name的元素
    let result = tagList.value.findIndex((item) => item.name === newTags.name);
    // 根据索引位置删除指定元素
    tagList.value.splice(result, 1);
  };

  return {
    tagList,
    setTagList,
    clearTags,
    removeTagList,
  };
});
